home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Point;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
-
- public class Animator extends Applet implements Runnable {
- Vector images;
- Hashtable durations;
- Hashtable sounds;
- Hashtable positions;
- URL backgroundImageURL;
- Image backgroundImage;
- URL startUpImageURL;
- Image startUpImage;
- URL soundtrackURL;
- AudioClip soundtrack;
- int maxWidth;
- int maxHeight;
- boolean imageLoadError = false;
- URL imageSource;
- URL soundSource;
- Thread engine;
- int frameNum;
- Integer frameNumKey;
- int xPos;
- int yPos;
- public static final int defaultPause = 3900;
- int globalPause = 3900;
- boolean userPause = false;
- boolean repeat;
- boolean loadFirst;
- Image offScrImage;
- Graphics offScrGC;
- boolean loaded = false;
- boolean error = false;
- static final String imageLabel = "image";
- static final String soundLabel = "sound";
- boolean debug = false;
-
- public String getAppletInfo() {
- return "Animator by Herb Jellinek";
- }
-
- public String[][] getParameterInfo() {
- String[][] info = new String[][]{{"imagesource", "url", "a directory"}, {"startup", "url", "displayed at startup"}, {"background", "url", "displayed as background"}, {"startimage", "int", "start index"}, {"endimage", "int", "end index"}, {"pause", "int", "milliseconds"}, {"pauses", "ints", "milliseconds"}, {"repeat", "boolean", "repeat or not"}, {"positions", "coordinates", "path"}, {"soundsource", "url", "audio directory"}, {"soundtrack", "url", "background music"}, {"sounds", "urls", "audio samples"}};
- return info;
- }
-
- void dbg(String s) {
- if (this.debug) {
- System.out.println(s);
- }
-
- }
-
- final int setFrameNum(int newFrameNum) {
- this.frameNumKey = new Integer(this.frameNum = newFrameNum);
- return this.frameNum;
- }
-
- public synchronized boolean imageUpdate(Image img, int infoFlags, int x, int y, int width, int height) {
- if ((infoFlags & 64) != 0) {
- this.imageLoadError = true;
- }
-
- this.notifyAll();
- return true;
- }
-
- void updateMaxDims(Dimension dim) {
- this.maxWidth = Math.max(dim.width, this.maxWidth);
- this.maxHeight = Math.max(dim.height, this.maxHeight);
- }
-
- Vector parseImages(String attr) {
- Vector result = new Vector(10);
-
- int next;
- for(int i = 0; i < attr.length(); i = next + 1) {
- next = attr.indexOf(124, i);
- if (next == -1) {
- next = attr.length();
- }
-
- String file = attr.substring(i, next);
- result.addElement(file);
- }
-
- return result;
- }
-
- URL fetchImages(Vector images) {
- for(int i = 0; i < images.size(); ++i) {
- Object o = images.elementAt(i);
- if (o instanceof URL) {
- URL url = (URL)o;
- this.tellLoadingMsg(url, "image");
- Image im = ((Applet)this).getImage(url);
-
- try {
- this.updateMaxDims(this.getImageDimensions(im));
- } catch (Exception var6) {
- return url;
- }
-
- images.setElementAt(im, i);
- }
- }
-
- return null;
- }
-
- Hashtable parseSounds(String attr, Vector images) throws MalformedURLException {
- Hashtable result = new Hashtable();
- int imageNum = 0;
- int numImages = images.size();
-
- for(int i = 0; i < attr.length() && imageNum < numImages; ++imageNum) {
- int next = attr.indexOf(124, i);
- if (next == -1) {
- next = attr.length();
- }
-
- String sound = attr.substring(i, next);
- if (sound.length() != 0) {
- result.put(new Integer(imageNum), new URL(this.soundSource, sound));
- }
-
- i = next + 1;
- }
-
- return result;
- }
-
- URL fetchSounds(Hashtable sounds) {
- Enumeration e = sounds.keys();
-
- while(e.hasMoreElements()) {
- Integer num = (Integer)e.nextElement();
- Object o = sounds.get(num);
- if (o instanceof URL) {
- URL file = (URL)o;
- this.tellLoadingMsg(file, "sound");
-
- try {
- sounds.put(num, ((Applet)this).getAudioClip(file));
- } catch (Exception var6) {
- return file;
- }
- }
- }
-
- return null;
- }
-
- Hashtable parseDurations(String attr, Vector images) {
- Hashtable result = new Hashtable();
- int imageNum = 0;
- int numImages = images.size();
-
- for(int i = 0; i < attr.length() && imageNum < numImages; ++imageNum) {
- int next = attr.indexOf(124, i);
- if (next == -1) {
- next = attr.length();
- }
-
- if (i != next - 1) {
- int duration = Integer.parseInt(attr.substring(i, next));
- result.put(new Integer(imageNum), new Integer(duration));
- } else {
- result.put(new Integer(imageNum), new Integer(this.globalPause));
- }
-
- i = next + 1;
- }
-
- return result;
- }
-
- Point parsePoint(String s) throws ParseException {
- int atPos = s.indexOf(64);
- if (atPos == -1) {
- throw new ParseException("Illegal position: " + s);
- } else {
- return new Point(Integer.parseInt(s.substring(0, atPos)), Integer.parseInt(s.substring(atPos + 1)));
- }
- }
-
- Hashtable parsePositions(String param, Vector images) throws ParseException {
- Hashtable result = new Hashtable();
- int imageNum = 0;
- int numImages = images.size();
-
- for(int i = 0; i < param.length() && imageNum < numImages; ++imageNum) {
- int next = param.indexOf(124, i);
- if (next == -1) {
- next = param.length();
- }
-
- if (i != next) {
- result.put(new Integer(imageNum), this.parsePoint(param.substring(i, next)));
- }
-
- i = next + 1;
- }
-
- return result;
- }
-
- synchronized Dimension getImageDimensions(Image im) throws ImageNotFoundException {
- while(true) {
- int width;
- if ((width = im.getWidth(this)) < 0) {
- try {
- this.wait();
- } catch (InterruptedException var5) {
- }
-
- if (!this.imageLoadError) {
- continue;
- }
-
- throw new ImageNotFoundException(im.getSource());
- }
-
- int height;
- while((height = im.getHeight(this)) < 0) {
- try {
- this.wait();
- } catch (InterruptedException var4) {
- }
-
- if (this.imageLoadError) {
- throw new ImageNotFoundException(im.getSource());
- }
- }
-
- return new Dimension(width, height);
- }
- }
-
- Vector prepareImageRange(int startImage, int endImage) throws MalformedURLException {
- Vector result = new Vector(Math.abs(endImage - startImage) + 1);
- if (startImage > endImage) {
- for(int i = startImage; i >= endImage; --i) {
- result.addElement(new URL(this.imageSource, "T" + i + ".gif"));
- }
- } else {
- for(int i = startImage; i <= endImage; ++i) {
- result.addElement(new URL(this.imageSource, "T" + i + ".gif"));
- }
- }
-
- return result;
- }
-
- public void init() {
- try {
- String param = ((Applet)this).getParameter("IMAGESOURCE");
- this.imageSource = param == null ? ((Applet)this).getDocumentBase() : new URL(((Applet)this).getDocumentBase(), param + "/");
- this.dbg("IMAGESOURCE = " + param);
- param = ((Applet)this).getParameter("PAUSE");
- this.globalPause = param != null ? Integer.parseInt(param) : 3900;
- this.dbg("PAUSE = " + param);
- param = ((Applet)this).getParameter("REPEAT");
- this.repeat = param == null ? true : param.equalsIgnoreCase("yes") || param.equalsIgnoreCase("true");
- int startImage = 1;
- int endImage = 1;
- param = ((Applet)this).getParameter("ENDIMAGE");
- this.dbg("ENDIMAGE = " + param);
- if (param != null) {
- endImage = Integer.parseInt(param);
- param = ((Applet)this).getParameter("STARTIMAGE");
- this.dbg("STARTIMAGE = " + param);
- if (param != null) {
- startImage = Integer.parseInt(param);
- }
-
- this.images = this.prepareImageRange(startImage, endImage);
- } else {
- param = ((Applet)this).getParameter("STARTIMAGE");
- this.dbg("STARTIMAGE = " + param);
- if (param != null) {
- startImage = Integer.parseInt(param);
- this.images = this.prepareImageRange(startImage, endImage);
- } else {
- param = ((Applet)this).getParameter("IMAGES");
- if (param == null) {
- ((Applet)this).showStatus("No legal IMAGES, STARTIMAGE, or ENDIMAGE specified.");
- return;
- }
-
- this.images = this.parseImages(param);
- }
- }
-
- param = ((Applet)this).getParameter("BACKGROUND");
- this.dbg("BACKGROUND = " + param);
- if (param != null) {
- this.backgroundImageURL = new URL(this.imageSource, param);
- }
-
- param = ((Applet)this).getParameter("STARTUP");
- this.dbg("STARTUP = " + param);
- if (param != null) {
- this.startUpImageURL = new URL(this.imageSource, param);
- }
-
- param = ((Applet)this).getParameter("SOUNDSOURCE");
- this.soundSource = param == null ? this.imageSource : new URL(((Applet)this).getDocumentBase(), param + "/");
- this.dbg("SOUNDSOURCE = " + param);
- param = ((Applet)this).getParameter("SOUNDS");
- this.dbg("SOUNDS = " + param);
- if (param != null) {
- this.sounds = this.parseSounds(param, this.images);
- }
-
- param = ((Applet)this).getParameter("PAUSES");
- this.dbg("PAUSES = " + param);
- if (param != null) {
- this.durations = this.parseDurations(param, this.images);
- }
-
- param = ((Applet)this).getParameter("POSITIONS");
- this.dbg("POSITIONS = " + param);
- if (param != null) {
- this.positions = this.parsePositions(param, this.images);
- }
-
- param = ((Applet)this).getParameter("SOUNDTRACK");
- this.dbg("SOUNDTRACK = " + param);
- if (param != null) {
- this.soundtrackURL = new URL(this.soundSource, param);
- }
- } catch (MalformedURLException e) {
- this.showParseError(e);
- } catch (ParseException e) {
- this.showParseError(e);
- }
-
- this.setFrameNum(0);
- }
-
- void tellLoadingMsg(String file, String fileType) {
- ((Applet)this).showStatus("Animator: loading " + fileType + " " + abridge(file, 20));
- }
-
- void tellLoadingMsg(URL url, String fileType) {
- this.tellLoadingMsg(url.toExternalForm(), fileType);
- }
-
- void clearLoadingMessage() {
- ((Applet)this).showStatus("");
- }
-
- static String abridge(String s, int len) {
- String ellipsis = "...";
- if (len >= s.length()) {
- return s;
- } else {
- int trim = len - ellipsis.length();
- return s.substring(0, trim / 2) + ellipsis + s.substring(s.length() - trim / 2);
- }
- }
-
- void loadError(URL badURL, String fileType) {
- String errorMsg = "Animator: Couldn't load " + fileType + " " + badURL.toExternalForm();
- ((Applet)this).showStatus(errorMsg);
- System.err.println(errorMsg);
- this.error = true;
- ((Component)this).repaint();
- }
-
- void showParseError(Exception e) {
- String errorMsg = "Animator: Parse error: " + e;
- ((Applet)this).showStatus(errorMsg);
- System.err.println(errorMsg);
- this.error = true;
- ((Component)this).repaint();
- }
-
- void startPlaying() {
- if (this.soundtrack != null) {
- this.soundtrack.loop();
- }
-
- }
-
- void stopPlaying() {
- if (this.soundtrack != null) {
- this.soundtrack.stop();
- }
-
- }
-
- public void run() {
- Thread me = Thread.currentThread();
- me.setPriority(1);
- if (!this.loaded) {
- try {
- if (this.startUpImageURL != null) {
- this.tellLoadingMsg(this.startUpImageURL, "image");
- this.startUpImage = ((Applet)this).getImage(this.startUpImageURL);
-
- try {
- this.updateMaxDims(this.getImageDimensions(this.startUpImage));
- } catch (Exception var12) {
- this.loadError(this.startUpImageURL, "start-up image");
- }
-
- ((Component)this).resize(this.maxWidth, this.maxHeight);
- ((Component)this).repaint();
- }
-
- if (this.backgroundImageURL != null) {
- this.tellLoadingMsg(this.backgroundImageURL, "image");
- this.backgroundImage = ((Applet)this).getImage(this.backgroundImageURL);
- ((Component)this).repaint();
-
- try {
- this.updateMaxDims(this.getImageDimensions(this.backgroundImage));
- } catch (Exception var11) {
- this.loadError(this.backgroundImageURL, "background image");
- }
- }
-
- URL badURL = this.fetchImages(this.images);
- if (badURL != null) {
- this.loadError(badURL, "image");
- return;
- }
-
- if (this.soundtrackURL != null && this.soundtrack == null) {
- this.tellLoadingMsg(this.soundtrackURL, "image");
- this.soundtrack = ((Applet)this).getAudioClip(this.soundtrackURL);
- if (this.soundtrack == null) {
- this.loadError(this.soundtrackURL, "soundtrack");
- return;
- }
- }
-
- if (this.sounds != null) {
- badURL = this.fetchSounds(this.sounds);
- if (badURL != null) {
- this.loadError(badURL, "sound");
- return;
- }
- }
-
- this.clearLoadingMessage();
- this.offScrImage = ((Component)this).createImage(this.maxWidth, this.maxHeight);
- this.offScrGC = this.offScrImage.getGraphics();
- this.offScrGC.setColor(Color.lightGray);
- ((Component)this).resize(this.maxWidth, this.maxHeight);
- this.loaded = true;
- this.error = false;
- } catch (Exception e) {
- this.error = true;
- ((Throwable)e).printStackTrace();
- }
- }
-
- if (!this.userPause) {
- if (this.repeat || this.frameNum < this.images.size()) {
- this.startPlaying();
- }
-
- try {
- if (this.images.size() > 1) {
- for(; this.maxWidth > 0 && this.maxHeight > 0 && this.engine == me; this.setFrameNum(this.frameNum + 1)) {
- if (this.frameNum >= this.images.size()) {
- if (!this.repeat) {
- return;
- }
-
- this.setFrameNum(0);
- }
-
- ((Component)this).repaint();
- if (this.sounds != null) {
- AudioClip clip = (AudioClip)this.sounds.get(this.frameNumKey);
- if (clip != null) {
- clip.play();
- }
- }
-
- try {
- Integer pause = null;
- if (this.durations != null) {
- pause = (Integer)this.durations.get(this.frameNumKey);
- }
-
- if (pause == null) {
- Thread.sleep((long)this.globalPause);
- } else {
- Thread.sleep((long)pause);
- }
- } catch (InterruptedException var10) {
- }
- }
-
- }
- } finally {
- this.stopPlaying();
- }
- }
- }
-
- public void paint(Graphics g) {
- if (!this.error && this.loaded) {
- if (this.images != null && this.images.size() > 0) {
- if (this.frameNum < this.images.size()) {
- if (this.backgroundImage == null) {
- this.offScrGC.fillRect(0, 0, this.maxWidth, this.maxHeight);
- } else {
- this.offScrGC.drawImage(this.backgroundImage, 0, 0, this);
- }
-
- Image image = (Image)this.images.elementAt(this.frameNum);
- Point pos = null;
- if (this.positions != null) {
- pos = (Point)this.positions.get(this.frameNumKey);
- }
-
- if (pos != null) {
- this.xPos = pos.x;
- this.yPos = pos.y;
- }
-
- this.offScrGC.drawImage(image, this.xPos, this.yPos, this);
- g.drawImage(this.offScrImage, 0, 0, this);
- return;
- }
-
- this.dbg("No more animation; drawing last image.");
- g.drawImage((Image)this.images.lastElement(), 0, 0, this);
- }
-
- } else if (this.startUpImage != null) {
- g.drawImage(this.startUpImage, 0, 0, this);
- } else if (this.backgroundImage != null) {
- g.drawImage(this.backgroundImage, 0, 0, this);
- } else {
- g.clearRect(0, 0, this.maxWidth, this.maxHeight);
- }
- }
-
- public void start() {
- if (this.engine == null) {
- this.engine = new Thread(this);
- this.engine.start();
- }
-
- }
-
- public void stop() {
- if (this.engine != null && this.engine.isAlive()) {
- this.engine.stop();
- }
-
- this.engine = null;
- }
-
- public boolean handleEvent(Event evt) {
- if (evt.id != 501) {
- return super.handleEvent(evt);
- } else {
- if (this.loaded) {
- if (this.engine != null && this.engine.isAlive()) {
- if (this.userPause) {
- this.engine.resume();
- this.startPlaying();
- } else {
- this.engine.suspend();
- this.stopPlaying();
- }
-
- this.userPause = !this.userPause;
- } else {
- this.userPause = false;
- this.setFrameNum(0);
- this.engine = new Thread(this);
- this.engine.start();
- }
- }
-
- return true;
- }
- }
- }
-